package de.koller.bubble.grow;

public class BubbleGrow {
	public static final int STEPS = 100000;
	private final double earthRadius = EarthLift.EARTH_RADIUS;

	private final Circle earth = new Circle( 0, 0, earthRadius );
	
	private double[][] intersectionAt = new double[STEPS][4];
	
	public static void main( String[] args ) {
		BubbleGrow b = new BubbleGrow();
		b.calcIntersectionAt();

		double t = ( b.earthRadius*2. * 1000. ) / ( 5 / 3.6 );
		
		double[] d = b.distances();
		for( int i = 0; i < d.length; i++ ) {
			double ct = t / STEPS * i;
			System.out.println( "[" + i + "] Nach " + 
					Math.round( ct / 60. / 60 * 100 ) / 100. + 
					" (" + Math.round( ct/ 60. / 60. / 24. *100. ) / 100. + " Tage) :" +
					( ( (d[i]*1000.) / (t / STEPS)) * 3.6 ));
		}
	}
	
	private double[] distances() {
		double[] d = new double[ intersectionAt.length ];
		d[0] = 0;
		for( int i = 1; i < d.length; i++ ) {
			double[] a = intersectionAt[i-1];
			double[] b = intersectionAt[i];
			double dx = b[0] - a[0];
			double dy = b[1] - a[1];
			d[i] = Math.sqrt( dx*dx + dy*dy );
		}
		return d;
	}
	
	private void calcIntersectionAt() {
		for( int k = 0; k < intersectionAt.length; k++ ) {
			double j = (earthRadius*2.) / intersectionAt.length * k;
			Circle bubble = new Circle( 0, earthRadius, j );
			intersectionAt[k] = intersection( earth, bubble );
		}
	}
	
	private double[] intersection( Circle c1, Circle c2 ) {
		double dx = c2.x - c1.x;
		double dy = c2.y - c1.y;
		double d = Math.sqrt( dx*dx + dy*dy );
		if( d > c1.r + c2.r )
			return null;
		if( d == 0 )
			return null;
		
		double cosalpha = ( c1.r*c1.r + d*d - c2.r*c2.r ) / ( 2 * c1.r * d );
		double uABx = ( c2.x - c1.x ) / d;
		double uABy = ( c2.y - c1.y ) / d;
		double puABx = uABy;
		double puABy = -uABx;
		
		double[] i = new double[4];
		i[0] = c1.x + uABx * (c1.r * cosalpha) + puABx * (c1.r * Math.sqrt( 1 - cosalpha*cosalpha ) );
		i[1] = c1.y + uABy * (c1.r * cosalpha) + puABy * (c1.r * Math.sqrt( 1 - cosalpha*cosalpha ) );
		
		i[2] = c1.x + uABx * (c1.r * cosalpha) - puABx * (c1.r * Math.sqrt( 1 - cosalpha*cosalpha ) );
		i[3] = c1.y + uABy * (c1.r * cosalpha) - puABy * (c1.r * Math.sqrt( 1 - cosalpha*cosalpha ) );
		
		return i;
	}
	
	private static class Circle { 
		private double r;
		private double x;
		private double y;

		private Circle( double x, double y, double r ) {
			this.r = r;
			this.x = x;
			this.y = y;
		}
		
	}
	
}
